import 'package:flutter/material.dart';
import '../static_var.dart';
import '../model/my_socket.dart';
import '../model/socket_msg.dart';
import './page_room_doudizhu.dart';
import '../widget/friends_list.dart';

class PageRoom extends StatefulWidget {
  final String roomId;
  final String roomType;
  final String roomPwd;
  final int needPlayerCount;
  final int antes;

  PageRoom({@required this.roomId, @required this.roomPwd, @required this.roomType, @required this.needPlayerCount, @required this.antes});

  @override
  _PageRoomState createState() {
    return _PageRoomState();
  }
}
class _PageRoomState extends State<PageRoom> {
  Widget roomContent;
  @override
  void initState() {
    super.initState();
    MySocket.sendMsg(type: SendMsgType.enterRoom, data: {'room_id': widget.roomId, 'room_pwd': widget.roomPwd});
  }

  showOutRoomDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (context){
        return AlertDialog(
          content: Text('确定离开？'),
          actions: <Widget>[
            FlatButton(child: Text('确定'), onPressed: (){
              MySocket.sendMsg(type: SendMsgType.outRoom);
              Navigator.of(context).pop();
              Navigator.of(context).pushReplacementNamed('/lobby');
            }),
            FlatButton(child: Text('取消', style: TextStyle(color: Colors.blueGrey),), onPressed: (){
              Navigator.of(context).pop();
            })
          ],
        );
      }
    );
  }

  void showFriendsDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return FriendsList(hasInviteBtn: true);
      }
    );
  }

  Widget roomTitle(BuildContext context) {
    TextStyle titleStyle = TextStyle(color: Colors.white, fontSize: 13);
    return Container(
      height: 30,
      color: Color.fromARGB(255, 90,122,225),
      padding: EdgeInsets.all(6),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Row(
              children: <Widget>[
                Text(StaticVar.roomType[widget.roomType], style: titleStyle),
                Container(width: 20),
                Text("房间号："+widget.roomId.toString(), style: titleStyle),
                Container(width: 20),
                Text("人数："+widget.needPlayerCount.toString(), style: titleStyle),
                Container(width: 20),
                Text("底注："+widget.antes.toString(), style: titleStyle),
                Container(width: 40),
                GestureDetector(
                  child: Icon(IconData(0xe60c, fontFamily: 'iconfont'), size: 16, color: Colors.white),
                  onTap: (){
                    showFriendsDialog(context);
                  },
                )
              ],
            ),
          ),
          FlatButton(child: Text('离开', style: titleStyle), onPressed: (){
            this.showOutRoomDialog(context);
          })
        ]
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    if(roomContent == null) {
      if(widget.roomType == 'doudizhu') {
        roomContent = PageRoomDoudizhu(antes: widget.antes);
      }
    }

    return WillPopScope(
      child: Column(
        children: <Widget>[
          this.roomTitle(context),
          Expanded(
            child: roomContent
          )
        ],
      ),
      onWillPop: (){
        this.showOutRoomDialog(context);
      }
    );
  }
}